<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>投票页面</title>
    <link
      rel="stylesheet"
      href="https://fonts.googleapis.com/css?family=Roboto:400,700"
    />
    <!-- https://fonts.google.com/specimen/Roboto -->
    <link rel="stylesheet" href="../css/fontawesome.min.css" />
    <!-- https://fontawesome.com/ -->
    <link rel="stylesheet" href="../css/bootstrap.min.css" />
    <!-- https://getbootstrap.com/ -->
    <link rel="stylesheet" href="../css/templatemo-style.css">
    <!--
	Product Admin CSS Template
	https://templatemo.com/tm-524-product-admin
	-->
  </head>
<script src="../js/jquery-1.7.2.js"></script>
<script type="text/javascript">
		let re = new RegExp("\s?" + "username" + "=([^;]+)(;|$)")
		var username=document.cookie.match(re)[1];
		/*var username=GetQueryString("username");
		function GetQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);

			if (r != null) 
				return decodeURI(r[2]);
			return null;
		}*/
		var arry;
		var arry2;
		var user_id;
		function test() {
            $.ajax({
                url: "http://localhost:8080/voting-system/getAllItem",
                type: "post",
                success:function (data) {
                	arry=data;
                    var tbhtml = "<table class=\"table table-hover tm-table-small tm-product-table\"><thead><tr><th scope=\"col\">&nbsp;</th><th scope=\"col\">序号</th><th scope=\"col\">投票标题</th><th scope=\"col\">投票内容</th><th scope=\"col\">投票类型</th><th scope=\"col\">&nbsp;</th></tr></thead><tbody>";
                    for(var i = 0;i<data.length;i++){
                    	var j=i+1;
                    	tbhtml += "<tr><th scope=\"row\"><input type=\"radio\" name=\"a\" value=\""+j+"\"/></th><td class=\"tm-product-name\">"+ data[i].item_id+"</td><td>"+data[i].item_title+"</td><td>"+data[i].item_text+"</td><td>";
                        if(data[i].item_type==0){
                        	tbhtml +="单选"+"</td></tr>";
                        }else
                        {
                        	tbhtml +="多选"+"</td></tr>";
                        }
                    }
                        tbhtml += "</tbody></table>";
                	$("#voteItem").html(tbhtml);
                	$.ajax({
			            url: "http://localhost:8080/voting-system/getUserId",
			            type: "post",
			            success:function (data) {
			            	arry2=data;
			            	user_id=arry2["UserId"];
			            	$.ajax({
			                    url: "http://localhost:8080/voting-system/getUser",
			                    type: "post",
			                    success:function (data) {
			                    	$("#name").html(data[0].username);
			                    	$("#sex").html(data[0].sex);
			                    	$("#age").html(data[0].age);
			                    	$("#tel").html(data[0].telephone);
			                    },
			                    dataType: "json",
			                    data:{user_id:user_id}
			                });
			            },
			            dataType: "json",
			            data:{username:username}
			        });
                },
                dataType: "json"
            });
           
        }
		function enterVote() {
			var value="";
			var radio=document.getElementsByName("a");
			for(var i=0;i<radio.length;i++){
				if(radio[i].checked==true){
					value=radio[i].value;
					break;
				}
			}
			document.cookie = 'item_id='+arry[value-1]["item_id"]+';'
			if(arry[value-1]["item_type"]==0){
				//window.location.href='radio.html?'+'item_id='+arry[value-1]["item_id"]+'&username='+username
				window.location.href='radio.html'
			}else if(arry[value-1]["item_type"]==1){
				//window.location.href='checkbox.html?'+'item_id='+arry[value-1]["item_id"]+'&username='+username
				window.location.href='checkbox.html'
			}
        }
</script>
  <body id="reportsPage" onload="test()">
    <nav class="navbar navbar-expand-xl">
      <div class="container h-100">
       
          <h1 class="tm-site-title mb-0">学生投票</h1>
       
        <button
          class="navbar-toggler ml-auto mr-0"
          type="button"
          data-toggle="collapse"
          data-target="#navbarSupportedContent"
          aria-controls="navbarSupportedContent"
          aria-expanded="false"
          aria-label="Toggle navigation"
        >
          <i class="fas fa-bars tm-nav-icon"></i>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mx-auto h-100">
            <li class="nav-item">
              <a class="nav-link" href="result.jsp">
                <i class="fas fa-tachometer-alt"></i>投票结果
                <span class="sr-only">(current)</span>
              </a>
            </li>
           
            <li class="nav-item">
              <a class="nav-link active" href="vote.html">
                <i class="fas fa-shopping-cart"></i> 学生投票
              </a>
            </li>

            <li class="nav-item">
              <a class="nav-link" href="personal.html">
                <i class="far fa-user"></i> 个人中心
              </a>
            </li>
           
          </ul>
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link d-block" href="../login.html">
              <b>退出登录</b>
              </a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    <form method="post">
    <div class="container mt-5" >
      <div class="row tm-content-row">
        <div class="col-sm-12 col-md-12 col-lg-8 col-xl-8 tm-block-col">
          <div class="tm-bg-primary-dark tm-block tm-block-products">
            <div class="tm-product-table-container" id="voteItem">
              <!-- <table class="table table-hover tm-table-small tm-product-table">
                <thead>
                  <tr>
                    <th scope="col">&nbsp;</th>
                    <th scope="col">序号</th>
                    <th scope="col">投票标题</th>
                    <th scope="col">投票内容</th>
                    <th scope="col">投票类型</th>
                    <th scope="col">&nbsp;</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row"><input type="radio" name="a" value="0"/></th>
                    <td class="tm-product-name"> 1</td>
                    <td>1,450</td>
                    <td>550</td>
                    <td>28 March 2019</td>
                    
                  </tr>
                  <tr>
                    <th scope="row"><input type="radio" name="a"/></th>
                    <td class="tm-product-name"> 2</td>
                    <td>1,250</td>
                    <td>750</td>
                    <td>21 March 2019</td>
                  
                  </tr>
                  <tr>
                    <th scope="row"><input type="radio" name="a" /></th>
                    <td class="tm-product-name"> 3</td>
                    <td>1,100</td>
                    <td>900</td>
                    <td>18 Feb 2019</td>
                   
                  </tr>
                  <tr>
                    <th scope="row"><input type="radio" name="a" /></th>
                    <td class="tm-product-name"> 4</td>
                    <td>1,400</td>
                    <td>600</td>
                    <td>24 Feb 2019</td>
                   
                  </tr>
                  <tr>
                    <th scope="row"><input type="radio" name="a"/></th>
                    <td class="tm-product-name"> 5</td>
                    <td>1,800</td>
                    <td>200</td>
                    <td>22 Feb 2019</td>
                  
                  </tr>
                  <tr>
                    <th scope="row"><input type="radio" name="a"/></th>
                    <td class="tm-product-name"> 6</td>
                    <td>1,000</td>
                    <td>1,000</td>
                    <td>20 Feb 2019</td>
                  
                  </tr>
                  <tr>
                    <th scope="row"><input type="radio" name="a"/></th>
                    <td class="tm-product-name"> 7</td>
                    <td>500</td>
                    <td>100</td>
                    <td>10 Feb 2019</td>
                  
                  </tr>
                  <tr>
                    <th scope="row"><input type="radio" name="a" /></th>
                    <td class="tm-product-name"> 8</td>
                    <td>1,000</td>
                    <td>600</td>
                    <td>08 Feb 2019</td>
                  
                 
                </tbody>
              </table>-->
            </div>
            <!-- table container -->
            <button class="btn btn-primary btn-block text-uppercase" type="button" onclick="enterVote()">
              进入投票
            </button>
          </div>
        </div>
        
        </form>
        <div class="col-sm-12 col-md-12 col-lg-4 col-xl-4 tm-block-col">
		<div class="tm-bg-primary-dark tm-block tm-block-product-categories">
			<h2 align="center" class="tm-block-title">学生</h2>
			<div class="form-group col-lg-6">
				<label for="name">用户名</label> <label name="name" id="name"
					style="width: 200px;" class="form-control validate"></label>
			</div>

			<div class="form-group col-lg-6">
				<label for="password">性别</label> <label name="sex" id="sex"
					style="width: 200px;" class="form-control validate"></label>
			</div>
			<div class="form-group col-lg-6">
				<label for="password2">年龄</label> <label name="age" id="age"
					style="width: 200px;" class="form-control validate"></label>
			</div>
			<div class="form-group col-lg-6">
				<label for="phone">电话</label> <label name="tel" id="tel"
					style="width: 200px;" class="form-control validate"></label>
			</div>
		</div>

	</div>
       


   
  </body>
</html>